<?php

namespace Summer\Location\Http\Controllers;

use Illuminate\Routing\Controller;
use Summer\Hub\Http\Controllers\Traits\ApiReturn;
use Summer\Location\Http\Requests\UserAddressCreateRequest;
use Summer\Location\Http\Requests\UserAddressUpdateRequest;
use Summer\Location\Models\AreaModel;
use Summer\Location\Models\CityModel;
use Summer\Location\Models\ProvinceModel;
use Summer\Location\Models\UserAddressModel;
use RLuders\JWTAuth\Classes\JWTAuth;
use Input;
use Db;

class UserAddressController extends Controller
{
    use ApiReturn;

    public function area(){
        $area_list=ProvinceModel::select('*','code AS value','name AS label')->with(
            ['cities'=>function($qc){
                $qc->select('*','code AS value','name AS label')->with([
                    'areas'=>function($qa){
                        $qa->select('*','code AS value','name AS label')->where('status',1);
                    }]
                )->where('status',1);
            }]
        )->where('status',1)->get();

        $return['provinceData']=$area_list;
        $return['cityData']=$area_list;
        $return['areaData']=$area_list;
        return $this->apiSuccess('操作成功',$return);
    }

    /**
     * 删除地址
     * @param JWTAuth $auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $useraddress_id=Input::get('id');
        if(!$useraddress_id){
            return $this->apiError('参数错误');
        }
        UserAddressModel::where('id',$useraddress_id)->where('user_id',$user->id)->delete();
        return $this->apiSuccess('操作成功');
    }
    /**
     * 修改地址
     * @param JWTAuth $auth
     * @param UserAddressUpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \RLuders\JWTAuth\Exceptions\JsonValidationException
     */
    public function update(JWTAuth $auth,UserAddressUpdateRequest $request){
        $request->validate();
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $useraddress_id=Input::get('id');
        $consignee=Input::get('consignee');
        $phone=Input::get('phone');
        $address=Input::get('address');
        $is_default=Input::get('is_default');
        $province_code=Input::get('province_code');
        $city_code=Input::get('city_code');
        $area_code=Input::get('area_code');
        $town_code=Input::get('town_code');
        $user_address=UserAddressModel::where('id',$useraddress_id)->where('user_id',$user->id)->first();
        if(!$user_address){
            return $this->apiError('查找失败');
        }
        $user_address->consignee=$consignee;
        $user_address->phone=$phone;
        $user_address->address=$address;
        $user_address->is_default=$is_default;
        $user_address->province_code=$province_code;
        $user_address->city_code=$city_code;
        $user_address->area_code=$area_code;
        $user_address->town_code=$town_code;
        $user_address->save();
        return $this->apiSuccess('操作成功',$user_address->id);
    }
    /**
     * 地址详情
     * @param JWTAuth $auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function preview(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $useraddress_id=Input::get('id');
        if(!$useraddress_id){
            return $this->apiError('参数错误');
        }
        $useraddress=UserAddressModel::with([
            'province'=>function($q){$q->select('code','name');},
            'city'=>function($q){$q->select('code','name');},
            'area'=>function($q){$q->select('code','name');},
            'town'=>function($q){$q->select('code','name');},
        ])->where('id',$useraddress_id)->where('user_id',$user->id)->first();
        if(!$useraddress){
            return $this->apiError('查找失败');
        }
        $useraddress->province_name=$useraddress->province->name;
        $useraddress->city_name=$useraddress->city->name;
        $useraddress->area_name=$useraddress->area->name;
        return $this->apiSuccess('操作成功',$useraddress);
    }
    /**
     * 地址列表
     * @param JWTAuth $auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $useraddresses=UserAddressModel::with([
            'province'=>function($q){$q->select('code','name');},
            'city'=>function($q){$q->select('code','name');},
            'area'=>function($q){$q->select('code','name');},
            'town'=>function($q){$q->select('code','name');},
        ])->where('user_id',$user->id)->get();
        $useraddresses->each(function ($useraddress){
            $useraddress->province_name=$useraddress->province->name;
            $useraddress->city_name=$useraddress->city->name;
            $useraddress->area_name=$useraddress->area->name;
        });
        return $this->apiSuccess('操作成功',$useraddresses);
    }
    /**
     * 添加地址
     * @param JWTAuth $auth
     * @param UserAddressCreateRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \RLuders\JWTAuth\Exceptions\JsonValidationException
     */
    public function create(JWTAuth $auth,UserAddressCreateRequest $request){
        $request->validate();
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $consignee=Input::get('consignee');
        $phone=Input::get('phone');
        $address=Input::get('address');
        $is_default=Input::get('is_default');
        $province_code=Input::get('province_code');
        $city_code=Input::get('city_code');
        $area_code=Input::get('area_code');
        $town_code=Input::get('town_code');
        $user_address=new UserAddressModel();
        $user_address->consignee=$consignee;
        $user_address->phone=$phone;
        $user_address->address=$address;
        $user_address->user_id=$user->id;
        $user_address->is_default=$is_default;
        $user_address->province_code=$province_code;
        $user_address->city_code=$city_code;
        $user_address->area_code=$area_code;
        $user_address->town_code=$town_code;
        $user_address->save();
        return $this->apiSuccess('操作成功',$user_address->id);
    }
    /**
     * 默认地址
     * @param JWTAuth $auth
     * @return \Illuminate\Http\JsonResponse
     */
    public function default(JWTAuth $auth){
        if (!$user = $auth->user()) {
            return $this->apiError('登录失效,请重新登录');
        }
        $useraddress=UserAddressModel::with([
            'province'=>function($q){$q->select('code','name');},
            'city'=>function($q){$q->select('code','name');},
            'area'=>function($q){$q->select('code','name');},
            'town'=>function($q){$q->select('code','name');},
        ])->where('is_default',1)->where('user_id',$user->id)->first();
        if(!$useraddress){
            return $this->apiSuccess('查找失败');
        }
        $useraddress->province_name=$useraddress->province->name;
        $useraddress->city_name=$useraddress->city->name;
        $useraddress->area_name=$useraddress->area->name;
        return $this->apiSuccess('操作成功',$useraddress);
    }
}
